import Store from '../store';
import { getdatabyselectionNoCopy } from './getdata';
import { isRealNull, isRealNum } from './validate';
import { update } from './format';
import locale from '../locale/locale';
import { isNil, isNumber } from 'lodash';

// 表格计数栏
export function countfunc () {
    if (!Store.luckysheet_select_save || Store.luckysheet_select_save.length == 0) {
        return;
    }

    let min = Infinity,   // 最小值
        max = -Infinity,  // 最大值
        sum = 0,          // 求和
        count = 0,        // 计数（非空单元格）
        sumCount = 0,
        mean = 0;         // 平均值
    let countMap = {};
    let useCt = null;
    for (let s = 0; s < Store.luckysheet_select_save.length; s++) {
        let data = getdatabyselectionNoCopy(Store.luckysheet_select_save[s]);
        const { row, column } = Store.luckysheet_select_save[s];
        for (let r = 0; r < data.length; r++) {
            for (let c = 0; c < data[0].length; c++) {
                if (isRealNull(data[r][c])) {
                    continue;
                }
                let value = data[r][c].v;
                if (value === '' || isNil(value)) continue;
                if (countMap[`${r + row[0]}-${c + column[0]}`]) continue;
                count++;
                if (data[r][c].ct != null && data[r][c].ct.t == "d") {
                    continue;
                }

                if (!isNumber(value)) {
                    continue;
                }
                if (!useCt) useCt = data[r][c].ct;
                value = parseFloat(value);

                sum += value;

                if (value < min) {
                    min = value;
                }

                if (value > max) {
                    max = value;
                }
                countMap[`${r + row[0]}-${c + column[0]}`] = 1;
                sumCount++;
            }
        }
    }
    let average = Math.round(sum / sumCount * 10000) / 10000;
    let locale_formula = locale().formula;
    if (useCt && useCt.fa) {
        sum = update(useCt.fa, sum);
        average = update(useCt.fa, average);
    }
    let ret = "";
    ret += "<span>" + locale_formula.count + ":" + count + "</span>";

    // 处理成亿万格式
    if (isFinite(max) || isFinite(min)) {
        ret += "<span>" + locale_formula.sum + ":" + sum + "</span>";
        ret += "<span>" + locale_formula.average + ":" + average + "</span>";
    }

    // if (isFinite(max)) {
    //     ret += "<span>" + locale_formula.max + ":" + update("w", max) + "</span>";
    // }

    // if (isFinite(min)) {
    //     ret += "<span>" + locale_formula.min + ":" + update("w", min) + "</span>";
    // }

    $("#luckysheet-sta-content").html(ret);
}
